package org.samoji.sji.model;
import java.math.BigDecimal;

import org.samoji.sji.profTokenizer.Token.Type;

public class AssignExpr extends Expr {
	private Type operation;
	private Id id;
	private Expr expr;
	
	public AssignExpr(Type operation, Id id, Expr expr) {
		this.operation = operation;
		this.id = id;
		this.expr = expr;
	}
	
	public static Expr _(Id id, Expr expr) {
		return new AssignExpr(Type.Becomes, id, expr);
	}
	
	public static Expr _(Type operation, Id id, Expr expr) {
		return new AssignExpr(operation, id, expr);
	}
	
	@Override
	public BigDecimal evaluate(Env env) {
		switch(operation) {
		case Becomes:
			env.put(id.getValue(), expr.evaluate(env));
			return env.get(id.getValue());
			
		case DivBecomes:
			env.put(id.getValue(), new DivideBinExpr(id, expr).evaluate(env));
			return env.get(id.getValue());

		case PlusBecomes:
			env.put(id.getValue(), new PlusBinExpr(id, expr).evaluate(env));
			return env.get(id.getValue());
		
		case MinusBecomes:
			env.put(id.getValue(), new MinusBinExpr(id, expr).evaluate(env));
			return env.get(id.getValue());

		case TimesBecomes:
			env.put(id.getValue(), new TimesBinExpr(id, expr).evaluate(env));
			return env.get(id.getValue());
		
		default:
			throw new RuntimeException("Assign operation not implemented");
		}
	}
}